package sg.edu.nus.iss.ooad.jass.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import sg.edu.nus.iss.ooad.jass.dao.ICompanyDAO;
import sg.edu.nus.iss.ooad.jass.dao.pattern.AbstractJASSDAOFactory;
import sg.edu.nus.iss.ooad.jass.entity.Company;

/**
 *
 * Company Controller. This class will handle all request related to the Company object.
 *
 * @author Isak Rabin (HT082193M)
 * @version 1.0
 * @since 1.0
 *private final static Logger logger
 * Creation / Modification History
 *         Isak Rabin        20-Sep-2009        Created
 *
 */
public class CompanyController {

        private final static Logger logger = Logger.getLogger(CompanyController.class.getName());

        /**
         * Advertiser Constructor
         */
        public CompanyController() {
        }

        /**
         * This operation will create Company into persistent data.
         * @param company
         * @return
         * @throws java.lang.Exception
         */
        public boolean createCompany(Company company) throws Exception {
                ICompanyDAO dao = null;
                boolean retValue = false;

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getCompanyDAO();
                        retValue = dao.createCompany(company);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                } finally {
                        dao = null;
                }

                return retValue;
        }

        /**
         * This operation will retrieve Company object from persistent.
         * @param id
         * @return
         * @throws java.lang.Exception
         */
        public Company retrieveCompany(String id) throws Exception {
                ICompanyDAO dao = null;
                Company retValue = null;

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getCompanyDAO();
                        retValue = dao.retrieveCompany(id);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                } finally {
                        dao = null;
                }

                return retValue;
        }

        /**
         *
         * @param company
         * @return
         * @throws java.lang.Exception
         */
        public boolean updateCompany(Company company) throws Exception {
                ICompanyDAO dao = null;
                boolean retValue = false;

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getCompanyDAO();
                        retValue = dao.updateCompany(company);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }

                return retValue;
        }

        /**
         *
         * @param company
         * @return
         * @throws java.lang.Exception
         */
        public boolean deleteCompany(Company company) throws Exception {
                ICompanyDAO dao = null;
                boolean retValue = false;

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getCompanyDAO();
                        retValue = dao.deleteCompany(company);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }

                return retValue;
        }

        /**
         *
         * @return
         * @throws java.lang.Exception
         */
        public List<Company> getAllCompany() throws Exception {

                ICompanyDAO dao = null;
                List<Company> retValue = new ArrayList<Company>();

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getCompanyDAO();
                        retValue = dao.getAllCompany();
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }

                return retValue;

        }

        /**
         * Load Company object from persistent and cache it.
         * @throws Exception
         */
        public synchronized static void cache() throws Exception {
                ICompanyDAO dao = null;
                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getCompanyDAO();
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }
        }
}
